﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Gift.DAL
{
	/// <summary>
	/// 数据访问类:DALNavigator
	/// </summary>
	public partial class DALNavigator
	{
		public DALNavigator()
		{}
		#region  Method
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int navigatorId)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from tbNavigator");
			strSql.Append(" where navigatorId=@navigatorId");
			SqlParameter[] parameters = {
					new SqlParameter("@navigatorId", SqlDbType.Int,4)
			};
			parameters[0].Value = navigatorId;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Gift.Model.tbNavigator model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into tbNavigator(");
            strSql.Append("parentId,navigatorName,navigatorOrder,depth,visible,navigatorLink,amount,system,navigatorDesc,navigatorContent,navigatorPic,navigatorKey,position,allowDeleted)");
			strSql.Append(" values (");
            strSql.Append("@parentId,@navigatorName,@navigatorOrder,@depth,@visible,@navigatorLink,@amount,@system,@navigatorDesc,@navigatorContent,@navigatorPic,@navigatorKey,@position,@allowDeleted)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@parentId", SqlDbType.Int,4),
					new SqlParameter("@navigatorName", SqlDbType.NVarChar,150),
					new SqlParameter("@navigatorOrder", SqlDbType.Int,4),
					new SqlParameter("@depth", SqlDbType.Int,4),
					new SqlParameter("@visible", SqlDbType.Bit,1),
					new SqlParameter("@navigatorLink", SqlDbType.NVarChar,150),
					new SqlParameter("@amount", SqlDbType.Int,4),
					new SqlParameter("@system", SqlDbType.Bit,1),
					new SqlParameter("@navigatorDesc", SqlDbType.NVarChar,250),
					new SqlParameter("@navigatorContent", SqlDbType.NVarChar),
					new SqlParameter("@navigatorPic", SqlDbType.NVarChar,150),
					new SqlParameter("@navigatorKey", SqlDbType.NVarChar,250),
                                        new SqlParameter("@position", SqlDbType.NVarChar,10),
                    	new SqlParameter("@allowDeleted", SqlDbType.Bit,1)};
			parameters[0].Value = model.parentId;
			parameters[1].Value = model.navigatorName;
			parameters[2].Value = model.navigatorOrder;
			parameters[3].Value = model.depth;
			parameters[4].Value = model.visible;
			parameters[5].Value = model.navigatorLink;
			parameters[6].Value = model.amount;
			parameters[7].Value = model.system;
			parameters[8].Value = model.navigatorDesc;
			parameters[9].Value = model.navigatorContent;
			parameters[10].Value = model.navigatorPic;
			parameters[11].Value = model.navigatorKey;
            parameters[12].Value = model.Position;
            parameters[13].Value = model.AllowDeleted;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Gift.Model.tbNavigator model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update tbNavigator set ");
			strSql.Append("parentId=@parentId,");
			strSql.Append("navigatorName=@navigatorName,");
			strSql.Append("navigatorOrder=@navigatorOrder,");
			strSql.Append("depth=@depth,");
			strSql.Append("visible=@visible,");
			strSql.Append("navigatorLink=@navigatorLink,");
			strSql.Append("amount=@amount,");
			strSql.Append("system=@system,");
			strSql.Append("navigatorDesc=@navigatorDesc,");
			strSql.Append("navigatorContent=@navigatorContent,");
			strSql.Append("navigatorPic=@navigatorPic,");
			strSql.Append("navigatorKey=@navigatorKey,");
            strSql.Append("position=@position,");
            strSql.Append("allowDeleted=@allowDeleted");
			strSql.Append(" where navigatorId=@navigatorId");
			SqlParameter[] parameters = {
					new SqlParameter("@parentId", SqlDbType.Int,4),
					new SqlParameter("@navigatorName", SqlDbType.NVarChar,150),
					new SqlParameter("@navigatorOrder", SqlDbType.Int,4),
					new SqlParameter("@depth", SqlDbType.Int,4),
					new SqlParameter("@visible", SqlDbType.Bit,1),
					new SqlParameter("@navigatorLink", SqlDbType.NVarChar,150),
					new SqlParameter("@amount", SqlDbType.Int,4),
					new SqlParameter("@system", SqlDbType.Bit,1),
					new SqlParameter("@navigatorDesc", SqlDbType.NVarChar,250),
					new SqlParameter("@navigatorContent", SqlDbType.NVarChar),
					new SqlParameter("@navigatorPic", SqlDbType.NVarChar,150),
					new SqlParameter("@navigatorKey", SqlDbType.NVarChar,250),
					new SqlParameter("@navigatorId", SqlDbType.Int,4),
                	new SqlParameter("@position", SqlDbType.NVarChar,10),
                    	new SqlParameter("@allowDeleted", SqlDbType.Bit,1)
                                        
                                        };
			parameters[0].Value = model.parentId;
			parameters[1].Value = model.navigatorName;
			parameters[2].Value = model.navigatorOrder;
			parameters[3].Value = model.depth;
			parameters[4].Value = model.visible;
			parameters[5].Value = model.navigatorLink;
			parameters[6].Value = model.amount;
			parameters[7].Value = model.system;
			parameters[8].Value = model.navigatorDesc;
			parameters[9].Value = model.navigatorContent;
			parameters[10].Value = model.navigatorPic;
			parameters[11].Value = model.navigatorKey;
			parameters[12].Value = model.navigatorId;
            parameters[13].Value = model.Position;
            parameters[14].Value = model.AllowDeleted;
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int navigatorId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from tbNavigator ");
			strSql.Append(" where navigatorId=@navigatorId");
			SqlParameter[] parameters = {
					new SqlParameter("@navigatorId", SqlDbType.Int,4)
			};
			parameters[0].Value = navigatorId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string navigatorIdlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from tbNavigator ");
			strSql.Append(" where navigatorId in ("+navigatorIdlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Gift.Model.tbNavigator GetModel(int navigatorId)
		{
			
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select  top 1 navigatorId,parentId,navigatorName,navigatorOrder,depth,visible,navigatorLink,amount,system,navigatorDesc,navigatorContent,navigatorPic,navigatorKey,position,isnull(allowDeleted,0) as allowDeleted from tbNavigator ");
			strSql.Append(" where navigatorId=@navigatorId");
			SqlParameter[] parameters = {
					new SqlParameter("@navigatorId", SqlDbType.Int,4)
			};
			parameters[0].Value = navigatorId;

			Gift.Model.tbNavigator model=new Gift.Model.tbNavigator();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["navigatorId"]!=null && ds.Tables[0].Rows[0]["navigatorId"].ToString()!="")
				{
					model.navigatorId=int.Parse(ds.Tables[0].Rows[0]["navigatorId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["parentId"]!=null && ds.Tables[0].Rows[0]["parentId"].ToString()!="")
				{
					model.parentId=int.Parse(ds.Tables[0].Rows[0]["parentId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["navigatorName"]!=null && ds.Tables[0].Rows[0]["navigatorName"].ToString()!="")
				{
					model.navigatorName=ds.Tables[0].Rows[0]["navigatorName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["navigatorOrder"]!=null && ds.Tables[0].Rows[0]["navigatorOrder"].ToString()!="")
				{
					model.navigatorOrder=int.Parse(ds.Tables[0].Rows[0]["navigatorOrder"].ToString());
				}
				if(ds.Tables[0].Rows[0]["depth"]!=null && ds.Tables[0].Rows[0]["depth"].ToString()!="")
				{
					model.depth=int.Parse(ds.Tables[0].Rows[0]["depth"].ToString());
				}
				if(ds.Tables[0].Rows[0]["visible"]!=null && ds.Tables[0].Rows[0]["visible"].ToString()!="")
				{
					if((ds.Tables[0].Rows[0]["visible"].ToString()=="1")||(ds.Tables[0].Rows[0]["visible"].ToString().ToLower()=="true"))
					{
						model.visible=true;
					}
					else
					{
						model.visible=false;
					}
				}
				if(ds.Tables[0].Rows[0]["navigatorLink"]!=null && ds.Tables[0].Rows[0]["navigatorLink"].ToString()!="")
				{
					model.navigatorLink=ds.Tables[0].Rows[0]["navigatorLink"].ToString();
				}
				if(ds.Tables[0].Rows[0]["amount"]!=null && ds.Tables[0].Rows[0]["amount"].ToString()!="")
				{
					model.amount=int.Parse(ds.Tables[0].Rows[0]["amount"].ToString());
				}
				if(ds.Tables[0].Rows[0]["system"]!=null && ds.Tables[0].Rows[0]["system"].ToString()!="")
				{
					if((ds.Tables[0].Rows[0]["system"].ToString()=="1")||(ds.Tables[0].Rows[0]["system"].ToString().ToLower()=="true"))
					{
						model.system=true;
					}
					else
					{
						model.system=false;
					}
				}
				if(ds.Tables[0].Rows[0]["navigatorDesc"]!=null && ds.Tables[0].Rows[0]["navigatorDesc"].ToString()!="")
				{
					model.navigatorDesc=ds.Tables[0].Rows[0]["navigatorDesc"].ToString();
				}
				if(ds.Tables[0].Rows[0]["navigatorContent"]!=null && ds.Tables[0].Rows[0]["navigatorContent"].ToString()!="")
				{
					model.navigatorContent=ds.Tables[0].Rows[0]["navigatorContent"].ToString();
				}
				if(ds.Tables[0].Rows[0]["navigatorPic"]!=null && ds.Tables[0].Rows[0]["navigatorPic"].ToString()!="")
				{
					model.navigatorPic=ds.Tables[0].Rows[0]["navigatorPic"].ToString();
				}
				if(ds.Tables[0].Rows[0]["navigatorKey"]!=null && ds.Tables[0].Rows[0]["navigatorKey"].ToString()!="")
				{
					model.navigatorKey=ds.Tables[0].Rows[0]["navigatorKey"].ToString();
				}
                if (ds.Tables[0].Rows[0]["position"] != null && ds.Tables[0].Rows[0]["position"].ToString() != "")
                {
                    model.Position = ds.Tables[0].Rows[0]["position"].ToString();
                }

                if (ds.Tables[0].Rows[0]["allowDeleted"] != null && ds.Tables[0].Rows[0]["allowDeleted"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["allowDeleted"].ToString() == "1") || (ds.Tables[0].Rows[0]["allowDeleted"].ToString().ToLower() == "true"))
                    {
                        model.AllowDeleted = true;
                    }
                    else
                    {
                        model.AllowDeleted = false;
                    }
                }
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select navigatorId,parentId,navigatorName,navigatorOrder,depth,visible,navigatorLink,amount,system,navigatorDesc,navigatorContent,navigatorPic,navigatorKey ");
            strSql.Append(", [navigatorName] + (select '[' + convert(nvarchar(10),count(1)) + ']' from tbNavigator g where g.parentId = tbNavigator.navigatorId ) as navigaterDec,isnull(position,'') as position ,isNull(allowDeleted,0) as allowDeleted  ");
			strSql.Append(" FROM tbNavigator ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            strSql.Append(" order by navigatorOrder asc ");
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" navigatorId,parentId,navigatorName,navigatorOrder,depth,visible,navigatorLink,amount,system,navigatorDesc,navigatorContent,navigatorPic,navigatorKey ");
			strSql.Append(" FROM tbNavigator ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM tbNavigator ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.navigatorId desc");
			}
			strSql.Append(")AS Row, T.*  from tbNavigator T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "tbNavigator";
			parameters[1].Value = "navigatorId";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

